/* 设置单个配置项 */
export async function saveUserProfile(profileKey, profileValue) {
  return new Promise(resolve => {
    if (typeof Funcs == 'undefined') {
      return resolve({ status: 'error', data: 'Funcs is undefined' });
    }
    return Funcs.SetUserProfile(profileKey, JSON.stringify(profileValue))
      .then(data => {
        if (data !== true && typeof data === 'string' && data !== '') {
          resolve({ status: 'error', data });
        } else {
          resolve({ status: 'success', data });
        }
      })
      .catch(err => resolve({ status: 'error', data: err }));
  });
}

/* 获取单个配置项 */
export async function getUserProfile(profileKey) {
  // Funcs.GetUserProfile = () => {
  //   return new Promise(resolve => {
  //     resolve([
  //       {
  //         UserID: 'c00c346f-4ecf-49ad-9d08-ca416b9189d8',
  //         ConfigID: 'tag-filter-undefined-undefined',
  //         DynamicConfigKey: '*',
  //         ConfigValue: '["DO_Sta.OperStartTime=1"]',
  //         TenantID: '*',
  //         PageID: '*',
  //         Ext1: null,
  //         Ext2: null,
  //         Ext3: null,
  //         ID: '74b2ff42-6092-46df-a47c-c23d6cc02596',
  //       },
  //     ]);
  //   });
  // };
  return new Promise(resolve => {
    if (typeof Funcs == 'undefined') {
      return resolve({ status: 'error', data: 'Funcs is undefined' });
    }
    return Funcs.GetUserProfile(profileKey)
      .then(({ data, errmsg }) => {
        if (Array.isArray(data) && data.length > 0) {
          const [config] = data;
          if (config.ConfigID == profileKey.Key && config.ConfigValue) {
            try {
              const ConfigValue = JSON.parse(config.ConfigValue);
              return resolve({ status: 'success', data: ConfigValue });
            } catch (e) {
              return resolve({ status: 'error', data: e });
            }
          }
        }

        resolve({ status: 'error', data: errmsg });
      })
      .catch(err => resolve({ status: 'error', data: err }));
  });
}

export default {
  saveUserProfile,
  getUserProfile,
};
